package com.bana.sys.controller;

import com.bana.sys.code.DatabaseResult;
import com.bana.sys.dao.CodeDAO;
import com.bana.sys.dao.CodeTypeDAO;
import com.bana.sys.model.Code;
import com.bana.sys.model.CodePageParameter;
import com.bana.sys.model.CodeType;
import com.bana.sys.utils.*;
import com.bana.sys.service.CodeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/code", method = RequestMethod.POST)
public class CodeController {

	@Resource
	private CodeDAO codeDAO;

	@Resource
	private CodeService codeService;

	@Resource
	private CodeTypeDAO codeTypeDAO;

	/**
	 * 查询字典集合
	 * @param request 查询参数 dmlb
	 * @return
	 */
	@RequestMapping(value = "/getCodes.do", method = RequestMethod.POST)
	public PageResponse<Code> getCodes(@RequestBody TokenRequest<CodePageParameter> request) {

		CodePageParameter pageParameter=request.getData();
		List<Code> codes=codeDAO.getCodes(pageParameter);
		int count=codeDAO.getCodeCount(pageParameter);
		return new PageResponse<>(codes,count);
	}

	/**
	 * 查询字典map
	 * @param request 查询参数 dmlb
	 * @return
	 */
	@RequestMapping(value = "/getCodeMap.do", method = RequestMethod.POST)
	public Map<String,String> getCodeMap(@RequestBody TokenRequest<CodePageParameter> request) {

		List<Code> codes=codeDAO.getCodes(request.getData());
		Map<String,String> map=new HashMap<>();
		for(Code code:codes)
		{
			map.put(Integer.toString(code.getDm()),code.getDmsm());
		}
		return map;
	}

	/**
	 * 查询字典类型集合
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCodeTypes.do", method = RequestMethod.POST)
	public List<CodeType> getCodeTypes(@RequestBody TokenRequest<Object> request){

		return codeTypeDAO.getCodeTypes();
	}

	/**
	 * 新增字典
	 * @param request 字典
	 * @return
	 */
	@RequestMapping(value = "/insertCode.do",method = RequestMethod.POST)
	public ActionResult insertCode(@RequestBody TokenRequest<Code> request){

		ActionResult actionResult = new ActionResult();
		codeService.insertCode(request.getData());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	/**
	 * 更新字典
	 * @param request 字典
	 * @return
	 */
	@RequestMapping(value = "/updateCode.do",method = RequestMethod.POST)
	public ActionResult updateCode(@RequestBody TokenRequest<Code> request){

		ActionResult actionResult = new ActionResult();
		codeService.updateCode(request.getData());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}

	/**
	 * 删除字典
	 * @param request 字典 dmlb 代码类别,dm 代码
	 * @return
	 */
	@RequestMapping(value = "/deleteCode.do",method = RequestMethod.POST)
	public ActionResult deleteCode(@RequestBody TokenRequest<Code> request){
		ActionResult actionResult = new ActionResult();
		codeService.deleteCode(request.getData());
		actionResult.setCzjg(DatabaseResult.Success);
		return actionResult;
	}
}